import numpy as np
import matplotlib.pyplot as plt

def logistic_function(x, r):
    return r * x * (1 - x)

def calculate_linear_correlation_coefficients(r, x0, iterations, lag):
    x1 = x0
    x2 = logistic_function(x0, r)
    
    correlation_coefficients = []
    
    for _ in range(iterations + lag):
        correlation_coefficients.append(np.corrcoef(x1, x2)[0, 1])
        x1 = logistic_function(x1, r)
        x2 = logistic_function(x2, r)
    
    return correlation_coefficients

def plot_linear_correlation_coefficients(r, x0, iterations, lag):
    correlation_coefficients = calculate_linear_correlation_coefficients(r, x0, iterations, lag)
    
    unique_coefficients, frequencies = np.unique(correlation_coefficients, return_counts=True)
    
    plt.bar(unique_coefficients, frequencies)
    plt.xlabel('Correlation Coefficient')
    plt.ylabel('Frequency')
    plt.title(f'Logistic Linear Correlation Coefficients Histogram (r={r}, x0={x0}, iterations={iterations})')
    plt.show()

# 设置参数
r = 3.9  # 控制增长率的参数
x0 = 0.2  # 初始状态值
iterations = 1000  # 迭代次数
lag = 50  # 延迟数

# 绘制图形
plot_linear_correlation_coefficients(r, x0, iterations, lag)
